package com.wxxymaker.edu.controller.api;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Param;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.LearnTask;
import com.wxxymaker.edu.model.entity.Task;
import com.wxxymaker.edu.model.entity.User;
import com.wxxymaker.edu.roles.StudentRoles;
import com.wxxymaker.edu.service.CourseService;
import com.wxxymaker.edu.service.LearnCourseService;
import com.wxxymaker.edu.service.LearnTaskService;
import com.wxxymaker.edu.service.TaskService;
import com.wxxymaker.edu.utils.TokenUtils;
import io.github.biezhi.anima.page.Page;

/**
 * @AUTHOR soft
 * @DATE 2018/9/16 15:22
 * @DESCRIBE 课程任务的一些查询接口
 */
@Path(value = "/course/task/api", restful = true)
public class TaskHandlerApi {

    @Inject
    private TaskService taskService;

    @Inject
    private LearnTaskService learnTaskService;

    @Inject
    private LearnCourseService learnCourseService;

    @Inject
    private CourseService courseService;

    /**
     * 将任务加入学习
     * @param kid 课程id
     * @param tid 任务id
     */
    @StudentRoles
    @PostRoute("/:kid/join/:tid")
    public RestResponse join(@PathParam Integer kid,
                             @PathParam Integer tid) {
        if (this.taskJoinLearn(kid, tid)) {
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 更新任务的学习情况
     * @param kid 课程id
     * @param ltid 学习任务 id
     * @param t 学习时间
     * @param r 学习完成度
     */
    @StudentRoles
    @PostRoute("/:kid/updateTime/:ltid")
    public RestResponse updateTime(@PathParam Integer kid,
                                   @PathParam Integer ltid,
                                   @Param String t,
                                   @Param Integer r) {
        if (learnTaskService.updateTime(ltid, Long.parseLong(t), r)) {
            if (r >= 100) {
                User user = TokenUtils.currentUser();
                if (user != null) {
                    learnCourseService.updateRate(kid, user.getId());
                }
            }
            return RestResponse.ok();
        }
        return RestResponse.fail();
    }

    /**
     * 课程任务分页查询 带学习进度
     * @param kid    课程id
     * @param status 任务状态 null 全部 0 未发布 1 发布
     * @param mater  是否带资源信息
     * @param page   页码
     * @param size   尺寸
     */
    @PostRoute("/:kid/page")
    public RestResponse page(@PathParam Integer kid,
                             @Param Integer status,
                             @Param boolean mater,
                             @Param(defaultValue = "1") int page,
                             @Param(defaultValue = "10") int size) {
        User user = TokenUtils.currentUser();
        if (user == null || user.getRole() < 2) { // 保证只有老师能看到未发布的任务
            status = 1;
        }

        Page<Task> page1 = null;
        if (user != null) {
            page1 = taskService.page(kid, user.getId(), status, mater, page, size);
        } else {
            page1 = taskService.page(kid, null, status, mater, page, size);
        }

        if (page1 != null) {
            return RestResponse.ok(page1);
        }
        return RestResponse.fail("这课程还没添加任何教学任务!");
    }

    /**
     * 获取一个任务 带着学习进度 不带资源
     * 获取单独的任务信息必须要学生以上权限
     * @param kid 课程id
     * @param tid 任务id
     * @param join 是否先将任务加入学习
     */
    @StudentRoles
    @PostRoute("/:kid/one/:tid")
    public RestResponse findOne(@PathParam Integer kid,
                                @PathParam Integer tid,
                                @Param Boolean join) {
        User user = TokenUtils.currentUser();
        if (user != null) {
            if (join) {
                this.taskJoinLearn(kid, tid);
            }
            Task one = taskService.findOne(tid, user.getId(), false);
            return RestResponse.ok(one);
        }
        return RestResponse.fail();
    }

    /**
     * 将任务加入学习
     * @param kid 课程id
     * @param tid 任务id
     */
    private boolean taskJoinLearn(int kid, int tid) {
        User user = TokenUtils.currentUser();
        if (user != null) {
            if (!courseService.isMyPublish(kid, user.getId())
                    && !learnTaskService.isJoin(user.getId(), tid) ) {
                LearnTask learnTask = new LearnTask();
                learnTask.setCourseId(kid);
                learnTask.setTaskId(tid);
                learnTask.setUserId(user.getId());
                learnTaskService.add(learnTask);
            }
            return true;
        }
        return false;
    }

//    /**
//     * 允许前三课的预览
//     * 必须是加入学习的课程才能获取到任务中的视频资源并查看
//     * @param kid 课程id
//     * @param tid 任务id
//     */
//    @PostRoute("/:kid/one/:tid")
//    public RestResponse one(@PathParam Integer kid,
//                            @PathParam Integer tid) {
//        List<Task> prev = taskService.findPrev(kid, 3);
//        Set<Task> collect = prev.stream()
//                .filter(task -> tid.equals(task.getId()))
//                .collect(Collectors.toSet());
//        User user = TokenUtils.currentUser();
//        if (!collect.isEmpty() ||
//                (user != null && learnCourseService.isJoin(kid, user.getId()))) {
//            Task one = taskService.findOne(tid, user.getId(),true);
//            if (one != null) return RestResponse.ok(one);
//        }
//        return RestResponse.fail();
//    }
}
